package com.hup.utils.commons;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Iterator;

/**
 * @author hugan
 * @date 2019/8/4
 */
@Log4j2
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TextUtil {

    public static String nullAtEmpty(Object o) {
        if (o == null) {
            return null;
        }
        String str = String.valueOf(o);
        return isBlank(str) ? null : str;
    }

    public static String emptyAtNull(Object o) {
        if (o == null) {
            return "";
        }
        return String.valueOf(o);
    }

    public static String getText(Object[] arr, String split) {
        return getText(Arrays.asList(arr), split);
    }

    public static <T> String getText(Iterable<T> iterable, RowHandler<T> handler) {
        return getText(iterable, null, null, null, handler);
    }

    public static <T> String getText(Iterable<T> iterable, String split) {
        return getText(iterable, split, null, null, null);
    }

    public static <T> String getText(Iterable<T> iterable, String split, String st, String ed) {
        return getText(iterable, split, st, ed, null);
    }

    public static <T> String getLineWrap(Iterable<T> iterable) {
        return getText(iterable, "\n", null, null, null);
    }

    public static <T> String getLineWrap(Iterable<T> iterable, RowHandler<T> handler) {
        return getText(iterable, "\n", null, null, handler);
    }

    /**
     * 换行的日志文本,每行会插入一个tab
     */
    public static <T> String getLineWrapLog(Iterable<T> iterable) {
        return getText(iterable, "\n\t", "\n\t", "", null);
    }

    /**
     * 换行的日志文本,每行会插入一个tab
     */
    public static <T> String getLineWrapLog(Iterable<T> iterable, RowHandler<T> handler) {
        return getText(iterable, "\n\t", "\n\t", "", handler);
    }

    /**
     * @param split nullable
     * @param st nullable
     * @param ed nullable
     * @param handler nullable
     */
    public static <T> String getText(Iterable<T> iterable, String split, String st, String ed, RowHandler<T> handler) {
        if (iterable == null) {
            return null;
        }
        Iterator<T> it = iterable.iterator();
        StringBuilder sb = new StringBuilder();
        if (!it.hasNext()) {
            if (st != null) sb.append(st);
            if (ed != null) sb.append(ed);
            return sb.toString();
        }

        if (st != null) sb.append(st);
        for (; ; ) {
            T next = it.next();
            boolean hasNext = it.hasNext();
            if (handler == null) {
                sb.append(next);
            } else {
                handler.append(sb, hasNext, next);
            }
            if (!hasNext) {
                if (ed != null) sb.append(ed);
                return sb.toString();
            }
            if (split != null) sb.append(split);
        }
    }

    public static boolean isBlank(String text) {
        //如果是'\u0000'(Unicode空格),则isBlank()的结果是false,要trim后才是true!
        return text == null || StringUtils.isBlank(text.trim());
    }

    /**
     * 获取字符串,去掉最后一个字符(通常是换行符等)
     * @param sb notNull
     */
    public static String getTextNotLastChar(StringBuilder sb) {
        if (sb.length() == 0) return "";
        sb.delete(sb.length() - 1, sb.length());
        return sb.toString();
    }

    public interface RowHandler<T> {
        void append(StringBuilder sb, boolean hasNext, T next);
    }

}
